﻿using JYH.Data;
using JYH.DataAccess;
using JYH.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace JYH.Business
{
    /// <summary>
    /// 采购入库
    /// </summary>
    public class WmsPurchaseReceiptOrderBLL : BusinessBase<WmsTradeOrderInfo>
    {
        public WmsPurchaseReceiptOrderBLL()
        {
        }

        /// <summary>
        /// 生产领料单同步
        /// </summary>
        /// <param name="Infos"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public static bool SyncPurchaseReceiptOrder(List<WmsTradeOrderInfo> Infos, out string Msg)
        {
            Msg = string.Empty;
            Database db = DB.D();
            Sql sql = new Sql();
            StringBuilder sb = new StringBuilder();
            db.BeginTransaction();
            try
            {
                string OrderNO = "'',";
                string MaterialNO = "''";
                string WarehouseNO = "'',";
                foreach (WmsTradeOrderInfo info in Infos)
                {
                    OrderNO += "'" + info.TradeOrderNO + "',";
                    WarehouseNO += "'" + info.FromWarehouseNO + "',";
                    foreach (WmsTradeSource source in info.TradeSources)
                    {
                        if (MaterialNO.IndexOf(source.MaterialNO) < 0)
                        {
                            MaterialNO += "'" + source.MaterialNO + "',";
                        }
                    }
                }
                OrderNO = OrderNO.Substring(0, OrderNO.Length - 1);
                WarehouseNO = WarehouseNO.Substring(0, WarehouseNO.Length - 1);
                MaterialNO = MaterialNO.Substring(0, MaterialNO.Length - 1);
                List<WmsTradeOrderInfo> OldOrders = new List<WmsTradeOrderInfo>();
                sql = new Sql();
                sql.Append(@";Select WmsTradeOrderInfo.*
                                From WmsTradeOrderInfo");
                sql.Where("WmsTradeOrderInfo.TradeOrderNO In (" + OrderNO + ")");
                sql.OrderBy("WmsTradeOrderInfo.TradeOrderNO Desc");
                OldOrders = db.Fetch<WmsTradeOrderInfo>(sql);
                List<WmsTradeSource> OldSouces = new List<WmsTradeSource>();
                sql = new Sql();
                sql.Append(@";Select WmsTradeSource.*,
                                WmsTradeOrderInfo.TradeOrderNO
                                From WmsTradeSource
                                Inner JOin   WmsTradeOrderInfo
                                ON WmsTradeOrderInfo.TradeOrderID=WmsTradeSource.TradeOrderID
                                ");
                sql.Where("WmsTradeOrderInfo.TradeOrderNO In (" + OrderNO + ")");
                sql.OrderBy("WmsTradeOrderInfo.TradeOrderNO Desc,WmsTradeSource.CreateTime");
                OldSouces = db.Fetch<WmsTradeSource>(sql);

                sql = new Sql();
                sql.Append(@";Select BasWarehouseInfo.* From BasWarehouseInfo");
                sql.Where("BasWarehouseInfo.WarehouseNO in (" + WarehouseNO + ")");
                sql.OrderBy("BasWarehouseInfo.WarehouseNO");
                List<BasWarehouseInfo> Warehouses = new List<BasWarehouseInfo>();
                Warehouses = db.Fetch<BasWarehouseInfo>(sql);
                sql = new Sql();
                sql.Append(@";Select BasStorageInfo.* From BasStorageInfo
                             Inner Join BasWarehouseInfo
                             On BasWarehouseInfo.WarehouseID=BasStorageInfo.WarehouseID");
                sql.Where("BasWarehouseInfo.WarehouseNO in (" + WarehouseNO + ")");
                sql.OrderBy("BasWarehouseInfo.WarehouseNO,BasStorageInfo.StorageNO");
                List<BasStorageInfo> Storages = new List<BasStorageInfo>();
                Storages = db.Fetch<BasStorageInfo>(sql);

                sql = new Sql();
                sql.Append(@";Select BasMaterialInfo.* From BasMaterialInfo");
                sql.Where("BasMaterialInfo.MaterialNO in (" + MaterialNO + ")");
                sql.OrderBy("BasMaterialInfo.MaterialNO");
                List<BasMaterialInfo> Materials = new List<BasMaterialInfo>();
                Materials = db.Fetch<BasMaterialInfo>(sql);
                int count = 0;
                for (int i = 0; i < Infos.Count; i++)
                {
                    int index = OldOrders.FindIndex(p => p.TradeOrderNO == Infos[i].TradeOrderNO);
                    if (index < 0)
                    {
                        WmsTradeOrderInfo info = new WmsTradeOrderInfo();
                        info.TradeOrderID = Guid.NewGuid().ToString();
                        info.TradeOrderNO = Infos[i].TradeOrderNO;
                        info.TradeDate = Infos[i].TradeDate;
                        info.RecvType = Infos[i].RecvType;
                        info.TradeType = Infos[i].TradeType;
                        BasWarehouseInfo w = Warehouses.FirstOrDefault(p => p.WarehouseNO == Infos[i].FromWarehouseNO);
                        if (w != null)
                        {
                            info.FromWarehouseID = w.WarehouseID;
                        }
                        info.CreateTime = BusinessTool.GetDateTime();
                        info.CreateUser = Infos[i].CreateUser;
                        info.RState = "0";
                        info.SourceOrderNO = Infos[i].SourceOrderNO;
                        OldOrders.Add(info);
                        count++;
                        sb.Append(BusinessTool.GetInsertSql(info));
                        for (int j = 0; j < Infos[i].TradeSources.Count; j++)
                        {
                            WmsTradeSource source = new WmsTradeSource();
                            source.ActionQty = 0;
                            source.TradeOrderID = info.TradeOrderID;
                            source.TradeSourceID = Guid.NewGuid().ToString();
                            source.RequestQty = Infos[i].TradeSources[j].RequestQty;
                            source.ErpItem = Infos[i].TradeSources[j].ErpItem;
                            source.BatchNO = Infos[i].TradeSources[j].BatchNO;
                            source.UnitNO = Infos[i].TradeSources[j].UnitNO;
                            source.SourceOrderItem = Infos[i].TradeSources[j].SourceOrderItem;
                            source.SourceOrderNO = Infos[i].TradeSources[j].SourceOrderNO;
                            source.POItem = Infos[i].TradeSources[j].POItem;
                            source.PONO = Infos[i].TradeSources[j].PONO;
                            BasMaterialInfo m = Materials.SingleOrDefault(p => p.MaterialNO == Infos[i].TradeSources[j].MaterialNO);
                            if (m != null)
                            {
                                source.MaterialID = m.MaterialID;
                            }
                            BasStorageInfo s = Storages.SingleOrDefault(p => p.StorageNO == Infos[i].Tradebarcodes[j].StorageNO);
                            if (s != null)
                            {
                                source.StorageID = s.StorageID;
                            }
                            sb.Append(BusinessTool.GetInsertSql(source));
                            count++;
                        }
                        if (count > 1000)
                        {
                            db.Execute(sb.ToString());
                            sb = new StringBuilder();
                            count = 0;
                        }
                    }
                    else
                    {
                        if (OldOrders[index].RState == "0")
                        {
                            //没有审核
                            sb.Append(@"Delete WmsTradeSource Where TradeOrderID='" + OldOrders[index].TradeOrderID + "'");

                            OldOrders[index].TradeOrderNO = Infos[i].TradeOrderNO;
                            OldOrders[index].TradeDate = Infos[i].TradeDate;
                            OldOrders[index].RecvType = Infos[i].RecvType;

                            BasWarehouseInfo w = Warehouses.FirstOrDefault(p => p.WarehouseNO == Infos[i].FromWarehouseNO);
                            if (w != null)
                            {
                                OldOrders[index].FromWarehouseID = w.WarehouseID;
                            }
                            OldOrders[index].UpdateTime = BusinessTool.GetDateTime();
                            OldOrders[index].UpdateUser = Infos[i].CreateUser;
                            OldOrders[index].RState = "0";
                            OldOrders[index].SourceOrderNO = Infos[i].SourceOrderNO;

                            count++;
                            sb.Append(BusinessTool.GetUpdateSql(OldOrders[index]));
                            for (int j = 0; j < Infos[i].TradeSources.Count; j++)
                            {
                                WmsTradeSource source = new WmsTradeSource();
                                source.ActionQty = 0;
                                source.TradeOrderID = OldOrders[index].TradeOrderID;
                                source.TradeSourceID = Guid.NewGuid().ToString();
                                source.RequestQty = Infos[i].TradeSources[j].RequestQty;
                                source.ErpItem = Infos[i].TradeSources[j].ErpItem;
                                source.BatchNO = Infos[i].TradeSources[j].BatchNO;
                                source.UnitNO = Infos[i].TradeSources[j].UnitNO;
                                source.SourceOrderItem = Infos[i].TradeSources[j].SourceOrderItem;
                                source.SourceOrderNO = Infos[i].TradeSources[j].SourceOrderNO;
                                source.POItem = Infos[i].TradeSources[j].POItem;
                                source.PONO = Infos[i].TradeSources[j].PONO;
                                source.StorageNO = Infos[i].TradeSources[j].StorageNO;
                                BasMaterialInfo m = Materials.SingleOrDefault(p => p.MaterialNO == Infos[i].TradeSources[j].MaterialNO);
                                if (m != null)
                                {
                                    source.MaterialID = m.MaterialID;
                                }
                                BasStorageInfo s = Storages.SingleOrDefault(p => p.StorageNO == Infos[i].Tradebarcodes[j].StorageNO);
                                if (s != null)
                                {
                                    source.StorageID = s.StorageID;
                                }
                                sb.Append(BusinessTool.GetInsertSql(source));
                                count++;
                            }
                            if (count > 1000)
                            {
                                db.Execute(sb.ToString());
                                sb = new StringBuilder();
                                count = 0;
                            }
                        }
                    }
                }

                if (sb.Length > 0)
                {
                    db.Execute(sb.ToString());
                    sb = new StringBuilder();
                    count = 0;
                }

                db.CompleteTransaction();
                return true;
            }
            catch (Exception ex)
            {
                Msg = ex.Message;
                db.AbortTransaction();
                return false;
            }
        }
    }
}